layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['transfer', 'form', 'jquery', 'table', 'laydate', 'util'], function () {
    var form = layui.form,
        transfer = layui.transfer,
        table = layui.table,
        laydate = layui.laydate,
        admin = layui.admin,
        util = layui.util;

    //执行一个laydate实例
    laydate.render({
        elem: '#created_time' //指定元素
    });

    table.render({
        elem: '#apply_table'
        , url: '/apply/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', title: 'id', width: 100, "align": "center", hide: true}
            , {field: 'oemId', width: 100, title: 'OEMID', "align": "center", hide: true}
            , {field: 'userId', width: 200, title: 'userId', "align": "center", hide: true}
            , {field: 'userName', width: 200, title: '申请人', "align": "center"}
            , {field: 'wcount', width: 150, title: '微信设备申请数', "align": "center"}
            , {field: 'acount', width: 150, title: '支付宝设备申请数', "align": "center"}
            , {
                field: 'state', width: 150, title: '状态', "align": "center", templet: function (d) {
                    if (d.state == 0) {
                        return "正在处理";
                    } else if (d.state == 1) {
                        return "已通过";
                    } else {
                        return "未通过";
                    }
                }
            }
            , {field: 'createdTime', width: 200, title: '申请时间', "align": "center"}
            , {field: 'OPT', width: 120, title: '操作', "align": "center", toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
        }
        , id: 'apply_table'
        , page: true
    });

    //查询按钮
    $("#apply_search_btn").click(function () {
        table.reload('apply_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                createdTime: $("#created_time").val()
            }
        });
    });

    /**
     * 按钮
     */
    table.on('tool(apply_table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'reject') { //驳回
            layer.open({
                type: 1,
                area: ['800px', '400px'],
                title: "批次详情",
                fixed: false, //不固定
                maxmin: true,
                maxmin: false,
                shadeClose: false,
                content: $('#add_apply_win')
            });
            //表单值
            form.val("add_apply_form", {
                'id': data.id
            });
        } else if (layEvent === 'part') {  //划拨
            //表单值
            form.val("apply_form", {
                'applyId': data.id,
                'userId': data.userId
            });
            $.ajax({
                url: "/apply/queryUsesOperDevices",
                data: {applyId: data.id},
                type: "post",
                success: function (result) {
                    console.log(result);
                    var data = result.data.userOperDevices;
                    var value = result.data.orderIds;
                    //实例调用
                    transfer.render({
                        elem: '#transfer'
                        , data: data
                        , id: 'transfer' //定义唯一索引
                        , showSearch: true
                        , title: ['目标', '已选择']
                        , width: 330
                        , height: 500
                        , text: {
                            none: '无设备' //没有数据时的文案
                            , searchNone: '无匹配设备' //搜索无匹配数据时的文案
                        }
                        , value: value
                        , parseData: function (res) {
                            return {
                                "value": res.id //数据值
                                , "title": res.deviceType == 'WECHAT' ? '微信:' + res.terminalSn : '支付宝:' + res.terminalSn //数据标题
                            }
                        }
                    });
                }
            });
            layer.open({
                type: 1,
                area: ['750px', '600px'],
                title: "设备划拨",
                fixed: false, //不固定
                maxmin: true,
                maxmin: false,
                shadeClose: false,
                content: $('#apply_win')
            });
        }else if(layEvent === 'details'){
            table.render({
                elem: '#applys_table'
                , url: '/apply/details'
                , method: 'post'
                , title: ''
                , height: 500
                , limit: 50							//显示数目
                , limits: [50, 100]
                , cols: [[
                    {field: 'id', title: 'id', width: 200, "align": "center", hide: true}
                    , {field: 'ownerName', title: '买方名称', width: 200, "align": "center"}
                    , {field: 'cameraSn', width: 220, title: '摄像头sn序列号', "align": "center"}
                    , {field: 'terminalSn', title: '设备sn序列号', width: 220, "align": "center"}
                    , {field: 'orgName', title: '归属公司名称', width: 200, "align": "center"}
                    , {field: 'deviceType', title: '设备类型', width: 150, "align": "center", templet: function (d) {
                            if (d.deviceType == "WECHAT") {
                                return "微信";
                            } else if (d.deviceType == "ALIPAY") {
                                return "支付宝";
                            } else{
                                return "";
                            }
                        }}
                    , {field: 'remarks', title: '备注', width: 140, "align": "center"}
                    , {field: 'createdTime', title: '创建时间', width: 180, "align": "center"}
                ]]
                , request: {
                    pageName: 'pageNo' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：LIMIT
                },
                where: {
                    applyId: data.id
                }
                , id: 'applys_table'
                , page: true
            });
            layer.open({
                type: 1,
                area: ['1500px', '650px'],
                title: "划拨详情",
                fixed: false, //不固定
                maxmin: true,
                maxmin: false,
                shadeClose: false,
                content: $('#apply')
            });
        }
    });

    //确认划拨
    $("#addOk").click(function () {
        //获得右侧数据
        var getData = transfer.getData('transfer');
        var transferData = JSON.stringify(getData);
        var applyId = $("#applyId").val();
        var userId = $("#userId").val();
        layer.confirm("请确认", function (index) {
            $.ajax({
                url: "/apply/transferDeviceInfo",
                data: {transferData: transferData,applyId: applyId,userId: userId},  //划拨设备数据 , 设备申请记录id, 设备申请人id
                type: "post",
                success: function (result) {
                    if (result.message == "操作成功") {
                        layer.msg("划拨成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('apply_table', {});
                                layer.closeAll();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        });
    });

    //暂存数据
    $("#deposit").click(function () {
        //获得右侧数据
        var getData = transfer.getData('transfer');
        var transferData = JSON.stringify(getData);
        var applyId = $("#applyId").val();
        layer.confirm("请确认", function (index) {
            $.ajax({
                url: "/apply/depositDeviceInfo",
                data: {transferData: transferData,applyId: applyId},  //划拨设备数据 , 设备申请记录id, 设备申请人id
                type: "post",
                success: function (result) {
                    if (result.message == "操作成功") {
                        layer.msg("暂存成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('apply_table', {});
                                layer.closeAll();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        });
    });

    /**
     * 新增提交
     */
    form.on('submit(addApplyOk)', function (data) {
        var id = $("#id").val();
        var remarks = $("#remarks").val();
        layer.confirm("请确认是否驳回!", function (index) {
            $.ajax({
                url: "/apply/rejectApplyInfo",
                data: {id: id, state: -1, remarks: remarks},  // 申请记录id ,设备申请记录状态 -1 不通过  备注
                type: "post",
                success: function (result) {
                    if (result.message == "操作成功") {
                        layer.msg("驳回成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('apply_table', {});
                                layer.closeAll();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        });
        return false;
    });

});

